package clang.functions;

import java.util.Vector;

import clang.data.VarType;
import clang.data.Variable;

public class FunctionSignature {
	protected Vector<String> parNames = new Vector<String>();
	protected Vector<VarType> parameters = new Vector<VarType>();
	private VarType retType;
	private String name;
	private boolean varArg = false;
	private int uid = -1;
	
	public FunctionSignature(Vector<VarType> parameters, VarType retType,
			String name, boolean varArg) {
		super();
		if(parameters != null)
			this.parameters = parameters;
		else
			this.parameters = new Vector<VarType>();
		this.retType = retType;
		this.name = name;
		this.varArg = varArg;
	}
	
	public FunctionSignature(Vector<VarType> parameters, VarType retType,
			String name, boolean varArg, int uid) {
		super();
		if(parameters != null)
			this.parameters = parameters;
		else
			this.parameters = new Vector<VarType>();
		this.retType = retType;
		this.name = name;
		this.varArg = varArg;
		this.uid = uid;
	}
	
	public void appendParameter(VarType param, String name)
	{
		parameters.add(param);
		parNames.add(name);
	}
	
	public boolean matches(FunctionCall fcall)
	{
		if(!name.equals(fcall.name)) return false;
		
		if(varArg)
		{
			if(fcall.parameters.size() < parameters.size()) return false;
		}
		else
		{
			if(fcall.parameters.size() != parameters.size()) return false;
		}
		
		for(int i = 0; i < parameters.size(); ++i)
		{
			if(parameters.get(i) != fcall.parameters.get(i).getType())
			{
				if(Variable.maxType(parameters.get(i), fcall.parameters.get(i).getType()) == 
					fcall.parameters.get(i).getType())
					return false;
			}
		}
		
		return true;
	}
	
	public void setReturnType(VarType retType) {
		this.retType = retType;
	}

	public VarType getReturnType() {
		return retType;
	}

	public String getName() {
		return name;
	}

	public boolean isVarArg() {
		return varArg;
	}

	public int getUid() {
		return uid;
	}
}
